package com.ruoyi.web.controller.trade;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.trade.domain.TransactionRecords;
import com.ruoyi.trade.service.ITransactionRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 成交记录Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/trade/tradeRecord")
public class TransactionRecordsController extends BaseController
{
    @Autowired
    private ITransactionRecordsService transactionRecordsService;

    /**
     * 查询成交记录列表
     */
    @PreAuthorize("@ss.hasPermi('trade:tradeRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(TransactionRecords transactionRecords)
    {
        startPage();
        List<TransactionRecords> list = transactionRecordsService.selectTransactionRecordsList(transactionRecords);
        return getDataTable(list);
    }

    /**
     * 导出成交记录列表
     */
    @PreAuthorize("@ss.hasPermi('trade:tradeRecord:export')")
    @Log(title = "成交记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TransactionRecords transactionRecords)
    {
        List<TransactionRecords> list = transactionRecordsService.selectTransactionRecordsList(transactionRecords);
        ExcelUtil<TransactionRecords> util = new ExcelUtil<TransactionRecords>(TransactionRecords.class);
        util.exportExcel(response, list, "成交记录数据");
    }

    /**
     * 获取成交记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('trade:tradeRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(transactionRecordsService.selectTransactionRecordsById(id));
    }

    /**
     * 新增成交记录
     */
    @PreAuthorize("@ss.hasPermi('trade:tradeRecord:add')")
    @Log(title = "成交记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TransactionRecords transactionRecords)
    {
        return toAjax(transactionRecordsService.insertTransactionRecords(transactionRecords));
    }

    /**
     * 修改成交记录
     */
    @PreAuthorize("@ss.hasPermi('trade:tradeRecord:edit')")
    @Log(title = "成交记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TransactionRecords transactionRecords)
    {
        return toAjax(transactionRecordsService.updateTransactionRecords(transactionRecords));
    }

    /**
     * 删除成交记录
     */
    @PreAuthorize("@ss.hasPermi('trade:tradeRecord:remove')")
    @Log(title = "成交记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(transactionRecordsService.deleteTransactionRecordsByIds(ids));
    }

    @Log(title = "交易记录管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('trade:tradeRecord:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TransactionRecords> util = new ExcelUtil<TransactionRecords>(TransactionRecords.class);
        List<TransactionRecords> tradeList = util.importExcel(file.getInputStream());
        String message = transactionRecordsService.importTradeRecords(tradeList,updateSupport);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TransactionRecords> util = new ExcelUtil<TransactionRecords>(TransactionRecords.class);
        util.importTemplateExcel(response, "交易记录数据");
    }

    @PostMapping("/assignClearGroup")
    public AjaxResult assignClearGroup(@RequestBody List<Long> ids) {
        try {
            int result = transactionRecordsService.assignClearGroup(ids);
            return AjaxResult.success("分配成功", result);
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/clearGroupNumber")
    public AjaxResult clearGroupNumber(@RequestBody List<Long> ids) {
        try {
            int result = transactionRecordsService.clearGroupNumber(ids);
            return AjaxResult.success("清除成功", result);
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
